#include "../head/client.h"
#include "../head/md5.h"

// 处理用户输入的指令
int handle(char *buf, int socket_fd, char *username, char *token, struct sockaddr_in *pserverAddr)
{
	train_t train;
	bzero(&train, sizeof(train));

	// 对用户输入的指令进行校验，最多只能有两个参数
	char *order = strtok(buf, " ");
	char *path = strtok(NULL, " ");
	char *arg1 = strtok(NULL, " ");

	if (arg1 != NULL)
	{
		NETDISK_LOG_ERR("Error Arguments");
		printf("Error Arguments!!\n");
		return -1;
	}

	char msg[1024] = {0};
	strcat(msg, "用户发送的命令为");
	strcat(msg, order);
	NETDISK_LOG_INFO_2(socket_fd, msg, username);
	bzero(msg, sizeof(msg));

	if (!strcmp(order, "cd"))
	{
		if (path == NULL)
			strcpy(path, "/");

		// send_str(socket_fd, username);
		send_str(socket_fd, order);
		send_str(socket_fd, username);
		send_str(socket_fd, path);
		// 是否cd成功
		bzero(&train, sizeof(train));
		recv(socket_fd, &train.length, sizeof(train.length), MSG_WAITALL);
		recv(socket_fd, train.data, train.length, MSG_WAITALL);
		int cmp = strcmp(train.data, "success");
		if (cmp == 0)
			printf("目录切换成功！\n");
		else
		{
			NETDISK_LOG_ERR("目录切换失败！您输入的路径不存在");
			printf("目录切换失败！您输入的路径不存在！\n");
		}
	}
	else if (!strcmp(order, "pwd"))
	{

		if (path != NULL)
		{
			NETDISK_LOG_ERR("Error Arguments");
			printf("Error Arguments!\n");
			return -1;
		}
		// send_str(socket_fd, username);
		send_str(socket_fd, order);
		send_str(socket_fd, username);
		bzero(&train, sizeof(train));
		recv(socket_fd, &train.length, sizeof(train.length), MSG_WAITALL);
		recv(socket_fd, train.data, train.length, MSG_WAITALL);
		puts(train.data); // 打印当前路径
	}
	else if (!strcmp(order, "ls"))
	{

		if (path != NULL)
		{
			NETDISK_LOG_ERR("Error Arguments");
			printf("Error Arguments!\n");
			return -1;
		}

		// send_str(socket_fd, username);
		send_str(socket_fd, order);
		send_str(socket_fd, username);

		bzero(&train, sizeof(train));
		recv(socket_fd, &train.length, sizeof(train.length), MSG_WAITALL);
		if (train.length == 0) // 空目录
		{
			return 0;
		}
		recv(socket_fd, train.data, train.length, MSG_WAITALL);
		// 打印当前目录下的所有文件名
		char *p;
		for (p = strtok(train.data, "/"); p != NULL; p = strtok(NULL, "/"))
		{
			printf("%s  ", p);
		}
		printf("\n");
	}
	else if (!strcmp(order, "puts"))
	{

		// 用户输入的指令必须有两个参数
		if (path == NULL)
		{
			NETDISK_LOG_ERR("Error Arguments");
			printf("Error Arguments!\n");
			return -1;
		}

		pthread_t tid_up;
		shareRes_t shareRes;
		bzero(&shareRes, sizeof(shareRes));
		strcpy(shareRes.order, order);
		strcpy(shareRes.path, path);
		strcpy(shareRes.token, token);
		strcpy(shareRes.username, username);
		memcpy(&shareRes.serverAddr, pserverAddr, sizeof(struct sockaddr_in));
		pthread_create(&tid_up, NULL, upload, &shareRes);
	}
	else if (!strcmp(order, "gets"))
	{

		// 用户输入的指令必须有两个参数
		if (path == NULL)
		{
			NETDISK_LOG_ERR("Error Arguments");
			printf("Error Arguments!\n");
			return -1;
		}

		pthread_t tid_down;
		shareRes_t shareRes;
		strcpy(shareRes.order, order);
		strcpy(shareRes.path, path);
		strcpy(shareRes.token, token);
		strcpy(shareRes.username, username);
		memcpy(&shareRes.serverAddr, pserverAddr, sizeof(struct sockaddr_in));
		pthread_create(&tid_down, NULL, download, &shareRes);
	}
	else if (!strcmp(order, "remove"))
	{

		if (path == NULL)
		{
			NETDISK_LOG_ERR("Error Arguments");
			printf("Error Arguments!\n");
			return -1;
		}

		// send_str(socket_fd, username);
		send_str(socket_fd, order);
		send_str(socket_fd, username);
		send_str(socket_fd, path);
		// 是否删除成功
		bzero(&train, sizeof(train));
		recv(socket_fd, &train.length, sizeof(train.length), MSG_WAITALL);
		recv(socket_fd, train.data, train.length, MSG_WAITALL);
		int cmp = strcmp(train.data, "success");
		if (cmp == 0)
			printf("删除文件成功！\n");
		else
		{
			NETDISK_LOG_ERR("删除文件失败！文件不存在！");
			printf("删除文件失败！文件不存在！\n");
		}
	}
	else
	{
		NETDISK_LOG_ERR("输入了错误的指令！");
		printf("输入了错误的指令!\n");
		return -1;
	}

	return 0;
}
